package com.example.student;

import android.database.Cursor;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.adapter.CourseAdapter;
import com.example.database.DatabaseHelper;


public class CourseSelection extends Fragment {

    private DatabaseHelper databaseHelper;
    private  Integer studentId;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_course_selection, container, false);
        // 获取传递的学号
        Bundle bundle = getArguments();
        //加载数据
        if (bundle != null) {
            this.studentId = bundle.getInt("student_id", -1);
            Log.d("student_id", String.valueOf(studentId));
        }
        // 获取课程数据
        DatabaseHelper dbHelper = new DatabaseHelper(getActivity());
        Cursor cursor = dbHelper.getAllCourses(String.valueOf(studentId));
//        // 检查Cursor是否为空
//        if (cursor != null && cursor.moveToFirst()) {
//            // 打印调试信息
//            String[] columnNames = cursor.getColumnNames();
//            for (String columnName : columnNames) {
//                int columnIndex = cursor.getColumnIndex(columnName);
//                if (columnIndex != -1) {
//                    Log.d("CursorDebug", columnName + ": " + cursor.getString(columnIndex));
//                }
//            }
//        } else {
//            Log.d("CursorDebug", "No data found in cursor");
//        }
        // 将数据显示到 RecyclerView
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        CourseAdapter adapter = new CourseAdapter(cursor, getActivity(), this.studentId);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);

        return view;

    }
}